﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TARS.User
{
    public partial class DeliveryMaster : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if(!Page.IsPostBack)
            if (Session["BranchesZoneID"].ToString().Trim().Length < 1)
            {
                loaddata();
            }
            else
            {
                loaddata2();
            }
        }
        protected void loaddata()
        {
            using (var db = new TARSDelivery_EprojectEntities())
            {
                var loadBranchesZone = db.t_BrachesZone.OrderByDescending(x=>x.ZoneName);
                cbbBrachesZone.DataSource = loadBranchesZone.ToList();
                cbbBrachesZone.DataValueField = "ID";
                cbbBrachesZone.DataTextField = "ZoneName";
                cbbBrachesZone.DataBind();

                Guid branches = new Guid(cbbBrachesZone.SelectedValue.ToString());
                var loadCarrierType = db.t_CarrierType.Where(x=>x.Active==true&&x.BranchesZoneID==branches).OrderBy(x=>x.CarrierTypeName);
                cbbCarrierType.DataSource = loadCarrierType.ToList();
                cbbCarrierType.DataTextField = "CarrierTypeName";
                cbbCarrierType.DataValueField = "ID";
                cbbCarrierType.DataBind();

                Guid carrierType = new Guid(cbbCarrierType.SelectedValue.ToString());
                var loadWeightType = db.t_WeightType.Where(x => x.CarrierTypeID == carrierType).OrderBy(x=>x.WeightTypeName);
                cbbWeightType.DataSource = loadWeightType.ToList();
                cbbWeightType.DataValueField = "ID";
                cbbWeightType.DataTextField = "WeightTypeName";
                cbbWeightType.DataBind();

                var loadDropZone = db.t_DropZone.Where(x => x.Active == true && x.CarrierTypeID == carrierType).OrderBy(x=>x.DropZoneName);
                cbbDropZone.DataSource = loadDropZone.ToList();
                cbbDropZone.DataValueField = "ID";
                cbbDropZone.DataTextField = "DropZoneName";
                cbbDropZone.DataBind();

                Guid weightType = new Guid(cbbWeightType.SelectedValue.ToString());
                Guid dropzone = new Guid(cbbDropZone.SelectedValue.ToString());
                var loadPrice = db.t_Prices.Where(x => x.DropZoneID == dropzone && x.WeightTypeID == weightType);
                cbbPrice.DataSource = loadPrice.ToList();
                cbbPrice.DataTextField = "Prices";
                cbbPrice.DataValueField = "ID";
                cbbPrice.DataBind();
            }
        }
        protected void loaddata2()
        {
            //Add Item Detail from Session to combobox in one by one
            using (var db = new TARSDelivery_EprojectEntities())
            {
                Guid brachesZone = new Guid(Session["BranchesZoneID"].ToString());
                var loadBranchesZone = db.t_BrachesZone;
                cbbBrachesZone.DataSource = loadBranchesZone.ToList().OrderBy(x=>x.ZoneName);
                cbbBrachesZone.DataValueField = "ID";
                cbbBrachesZone.DataTextField = "ZoneName";
                cbbBrachesZone.SelectedValue = brachesZone.ToString();
                cbbBrachesZone.DataBind();

                Guid carriertype = new Guid(Session["CarrierTypeID"].ToString());
                Guid branches = new Guid(cbbBrachesZone.SelectedValue.ToString());
                var loadCarrierType = db.t_CarrierType.Where(x => x.Active == true && x.BranchesZoneID == branches).OrderBy(x=>x.CarrierTypeName);
                cbbCarrierType.DataSource = loadCarrierType.ToList();
                cbbCarrierType.DataTextField = "CarrierTypeName";
                cbbCarrierType.DataValueField = "ID";
                cbbCarrierType.SelectedValue = carriertype.ToString();
                cbbCarrierType.DataBind();

                Guid weightType = new Guid(Session["WeightTypeID"].ToString());
                Guid carrierType = new Guid(cbbCarrierType.SelectedValue.ToString());
                var loadWeightType = db.t_WeightType.Where(x => x.CarrierTypeID == carrierType).OrderBy(x=>x.WeightTypeName);
                cbbWeightType.DataSource = loadWeightType.ToList();
                cbbWeightType.DataValueField = "ID";
                cbbWeightType.DataTextField = "WeightTypeName";
                cbbWeightType.SelectedValue = weightType.ToString();
                cbbWeightType.DataBind();


                Guid dropZone = new Guid(Session["DropZoneID"].ToString());
                var loadDropZone = db.t_DropZone.Where(x => x.Active == true && x.CarrierTypeID == carrierType).OrderBy(x=>x.DropZoneName);
                cbbDropZone.DataSource = loadDropZone.ToList();
                cbbDropZone.DataValueField = "ID";
                cbbDropZone.DataTextField = "DropZoneName";
                cbbDropZone.SelectedValue = dropZone.ToString();
                cbbDropZone.DataBind();

                Guid price = new Guid(Session["PriceID"].ToString());
                var loadPrice = db.t_Prices.Where(x => x.DropZoneID == dropZone && x.WeightTypeID == weightType);
                cbbPrice.DataSource = loadPrice.ToList();
                cbbPrice.DataTextField = "Prices";
                cbbPrice.DataValueField = "ID";
                cbbPrice.SelectedValue = price.ToString();
                cbbPrice.DataBind();


                cbbInsurance.SelectedValue = Session["Insurance"].ToString();
                
                //Add Session Item Detail  to TextBox         
                txtItemDetail.Text = Session["ItemDetail"].ToString();
                txtQuanlity.Text = Session["Quanlity"].ToString();
                txtWeight.Text = Session["Weight"].ToString();

                //Add Session Delivery Detail
                txtDeliveryFName.Text = Session["DeliveryFName"].ToString();
                txtDeliveryLName.Text = Session["DeliveryLName"].ToString();
                txtDeliveryAddress.Text = Session["DeliveryAddress"].ToString();
                txtDeliveryCity.Text = Session["DeliveryCity"].ToString();
                txtDeliveryState.Text = Session["DeliveryState"].ToString();
                txtDeliveryContactNumber.Text = Session["DeliveryContactNumber"].ToString();
                txtDeliveryZipCode.Text = Session["DeliveryZipCode"].ToString();
                //Add Session Customer Detail
                txtCustomerFName.Text = Session["CustomerFName"].ToString();
                txtCustomerLName.Text = Session["CustomerLName"].ToString();
                txtCustomerAddress.Text = Session["CustomerAddress"].ToString();
                txtCustomerCity.Text = Session["CustomerCity"].ToString();
                txtCustomerState.Text = Session["CustomerState"].ToString();
                txtCustomerContactNumber.Text = Session["CustomerContactNumber"].ToString();
                txtCustomerZipCode.Text = Session["CustomerZipCode"].ToString();
                txtCustomerEmail.Text = Session["CustomerEmail"].ToString();
            }
        }

        protected void cbbBrachesZone_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            using (var db = new TARSDelivery_EprojectEntities())
            {
                Guid branchesZone = new Guid(e.Value);
                var loadCarrierType = db.t_CarrierType.Where(x => x.Active == true && x.BranchesZoneID == branchesZone).OrderBy(x=>x.CarrierTypeName);
                cbbCarrierType.DataSource = loadCarrierType.ToList();
                cbbCarrierType.DataTextField = "CarrierTypeName";
                cbbCarrierType.DataValueField = "ID";
                cbbCarrierType.DataBind();

                Guid carrierType = new Guid(cbbCarrierType.SelectedValue.ToString());
                var loadWeightType = db.t_WeightType.Where(x => x.CarrierTypeID == carrierType).OrderBy(x=>x.WeightTypeName);
                cbbWeightType.DataSource = loadWeightType.ToList();
                cbbWeightType.DataValueField = "ID";
                cbbWeightType.DataTextField = "WeightTypeName";
                cbbWeightType.DataBind();

                var loadDropZone = db.t_DropZone.Where(x => x.Active == true && x.CarrierTypeID == carrierType).OrderBy(x=>x.DropZoneName);
                cbbDropZone.DataSource = loadDropZone.ToList();
                cbbDropZone.DataValueField = "ID";
                cbbDropZone.DataTextField = "DropZoneName";
                cbbDropZone.DataBind();

                Guid weightType = new Guid(cbbWeightType.SelectedValue.ToString());
                Guid dropzone = new Guid(cbbDropZone.SelectedValue.ToString());
                var loadPrice = db.t_Prices.Where(x => x.DropZoneID == dropzone && x.WeightTypeID == weightType);
                cbbPrice.DataSource = loadPrice.ToList();
                cbbPrice.DataTextField = "Prices";
                cbbPrice.DataValueField = "ID";
                cbbPrice.DataBind();
            }
        }

        protected void cbbCarrierType_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            using (var db = new TARSDelivery_EprojectEntities())
            {
                Guid carrierType = new Guid(e.Value);
                var loadWeightType = db.t_WeightType.Where(x => x.CarrierTypeID == carrierType).OrderBy(x => x.WeightTypeName).OrderBy(x=>x.WeightTypeName) ;
                cbbWeightType.DataSource = loadWeightType.ToList();
                cbbWeightType.DataValueField = "ID";
                cbbWeightType.DataTextField = "WeightTypeName";
                cbbWeightType.DataBind();

                var loadDropZone = db.t_DropZone.Where(x => x.Active == true && x.CarrierTypeID == carrierType).OrderBy(x=>x.DropZoneName);
                cbbDropZone.DataSource = loadDropZone.ToList();
                cbbDropZone.DataValueField = "ID";
                cbbDropZone.DataTextField = "DropZoneName";
                cbbDropZone.DataBind();

                Guid dropzon = new Guid(cbbDropZone.SelectedValue.ToString());
                Guid weight = new Guid (cbbWeightType.SelectedValue.ToString());
                var loadPrice = db.t_Prices.Where(x => x.DropZoneID == dropzon && x.WeightTypeID == weight);
                cbbPrice.DataSource = loadPrice.ToList();
                cbbPrice.DataTextField = "Prices";
                cbbPrice.DataValueField = "ID";
                cbbPrice.DataBind();
            }
        }

        protected void cbbWeightType_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            using (var db = new TARSDelivery_EprojectEntities())
            {
                Guid weightType = new Guid(e.Value);
                Guid dropZone = new Guid(cbbDropZone.SelectedValue.ToString());
                var loadPrice = db.t_Prices.Where(x => x.WeightTypeID == weightType && x.DropZoneID == dropZone);
                cbbPrice.DataSource = loadPrice.ToList();
                cbbPrice.DataTextField = "Prices";
                cbbPrice.DataValueField = "ID";
                cbbPrice.DataBind();              
            }
        }

        protected void cbbDropZone_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            using (var db = new TARSDelivery_EprojectEntities())
            {
                Guid dropZone = new Guid(e.Value);
                Guid weightType = new Guid(cbbWeightType.SelectedValue.ToString());
                var loadPrice = db.t_Prices.Where(x => x.WeightTypeID == weightType && x.DropZoneID == dropZone);
                cbbPrice.DataSource = loadPrice.ToList();
                cbbPrice.DataTextField = "Prices";
                cbbPrice.DataValueField = "ID";
                cbbPrice.DataBind();
            }
        }

        protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            //Begin Add Detail to Session for save :
            string totalWeight=(Convert.ToDouble(txtWeight.Text)*Convert.ToDouble(txtQuanlity.Text)).ToString();  
            //Item Detail      
            Session["BranchesZoneID"] = cbbBrachesZone.SelectedValue.ToString();
            Session["CarrierTypeID"] = cbbCarrierType.SelectedValue.ToString();
            Session["WeightTypeID"] = cbbWeightType.SelectedValue.ToString();
            Session["DropZoneID"] = cbbDropZone.SelectedValue.ToString();
            Session["totalWeight"] = totalWeight.ToString();
            Session["ItemDetail"] = txtItemDetail.Text;
            Session["Weight"] = txtWeight.Text;
            Session["Quanlity"] = txtQuanlity.Text;
            Session["PriceID"] = cbbPrice.SelectedValue.ToString();
            Session["Insurance"] = cbbInsurance.SelectedValue.ToString();
           
            //Delivery Detail
            Session["DeliveryFName"] = txtDeliveryFName.Text;
            Session["DeliveryLName"] = txtDeliveryLName.Text;
            Session["DeliveryAddress"] = txtDeliveryAddress.Text;
            Session["DeliveryCity"] = txtDeliveryCity.Text;
            Session["DeliveryState"] = txtDeliveryState.Text;
            Session["DeliveryContactNumber"] = txtDeliveryContactNumber.Text;
            Session["DeliveryZipCode"] = txtDeliveryZipCode.Text;
            //Customer Detail
            Session["CustomerFName"] = txtCustomerFName.Text;
            Session["CustomerLName"] = txtCustomerLName.Text;
            Session["CustomerAddress"] = txtCustomerAddress.Text;
            Session["CustomerCity"] = txtCustomerCity.Text;
            Session["CustomerState"] = txtCustomerState.Text;
            Session["CustomerContactNumber"] = txtCustomerContactNumber.Text;
            Session["CustomerZipCode"] = txtCustomerZipCode.Text;
            Session["CustomerEmail"] = txtCustomerEmail.Text;

            //move to review order
            Response.Redirect("ReviewDeliveryMaster.aspx");
          
        }

        protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            e.Cancel = true;
        }

        protected void cbbPrice_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }     
    }
}